exports.salesIndex = function(req,res){

    var os_id = req.headers.os_id;     //当前节点ID

    var resultData = {};

    //获取当前节点下的所有销售人员
    function searchNodeSales(cb){

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_master_osid/" + os_id + "/skip/0/limit/999",
                method : 'POST',
                data : {
                    fields : `
                        u_id,
                        u_nickname,
                        u_phone,
                        u_read_count
                    `
                }
            },
            function (data) {

                var userArr = data.result.data;

                resultData.salesData = userArr;

                cb();
                
            }
        )

    }

    //查询每个销售的订单相关信息
    function searchSalesOrder(cb){

        F.SerialEach(resultData.salesData,function(item,index,callback){

            //获取该代理下所有销售的订单数据
            R.SEND_HTTP(
                req,
                res,
                {
                    url: config.external_server.apiAddress + "/organization/search_art_product_order/apo_status/1/apo_invite_code/" + item.u_id,
                    method : 'POST',
                    data   : {
                        fields : `apo_real_pay`
                    }
                },
                function (data) {

                    //计算成交总数 和 成交金额
                    var buyCount = data.result.data.length;
                    var buyMoney = 0.0;

                    for(var i=0;i<data.result.data.length;i++){
                        buyMoney += parseFloat(data.result.data[i].apo_real_pay);
                    }

                    item.buyCount = buyCount;
                    item.buyMoney = buyMoney.toFixed(2);
                
                    callback();
                    
                }
            )

        },cb)

    }

    //计算所有销售的总成交数 和 成交金额
    function searchAllSalesOrder(cb){

        var allBuyCount  = 0;
        var allBuyMoney  = 0.0;
        var allReadCount = 0;

        for(var i=0;i<resultData.salesData.length;i++){

            allBuyCount  += parseInt(resultData.salesData[i].buyCount);
            allBuyMoney  += parseFloat(resultData.salesData[i].buyMoney);
            allReadCount += parseInt(resultData.salesData[i].u_read_count);

        }

        resultData.allBuyCount  = allBuyCount;
        resultData.allBuyMoney  = allBuyMoney;
        resultData.allReadCount = allReadCount;

        cb();

    }


    F.Serial([searchNodeSales,searchSalesOrder,searchAllSalesOrder],() => {
        RES.successResponse(res,resultData);
    })
    

}